﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Utilities;
using VOL.Domain.Dtos;
using VOL.Domain.Infrastructure;
using VOL.Domain.IServices.WarehouseManagement;
using VOL.Entity.DomainModels;

namespace VOL.Domain.Services.WarehouseManagement
{
    /// <summary>
    /// 到货通知业务实现类
    /// </summary>
    public class InformService : IInformService
    {
        /// <summary>
        /// 到货通知仓储
        /// </summary>
        private readonly IBaseRepository<Inform> _inform;
        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly IBaseRepository<Supplier> _supplier;
        /// <summary>
        /// 入库单仓储
        /// </summary>
        private readonly IBaseRepository<PurchaseWarehousing> _warehousing;

        /// <summary>
        /// 构造函数，注入仓储依赖
        /// </summary>
        /// <param name="inform">到货通知仓储</param>
        /// <param name="supplier">供应商仓储</param>
        /// <param name="warehousing">入库单仓储</param>
        public InformService(
            IBaseRepository<Inform> inform,
            IBaseRepository<Supplier> supplier,
            IBaseRepository<PurchaseWarehousing> warehousing)
        {
            _inform = inform;
            _supplier = supplier;
            _warehousing = warehousing;
        }

        /// <summary>
        /// 新增到货通知
        /// </summary>
        /// <param name="inform">到货通知实体</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> AddInformAsync(Inform inform)
        {
            var result = await _inform.Add(inform);
            return result > 0 ? WebResponseContent.Instance.OK("添加成功") : WebResponseContent.Instance.Error("添加失败");
        }

        /// <summary>
        /// 批量删除到货通知
        /// </summary>
        /// <param name="ids">要删除的ID数组</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> DeleteInformsAsync(int[] ids)
        {
            var toDelete = _inform.GetAll(x => ids.Contains(x.Id)).ToList();
            var result = await _inform.DeleteRang(toDelete);
            return result > 0 ? WebResponseContent.Instance.OK("删除成功") : WebResponseContent.Instance.Error("删除失败");
        }

        /// <summary>
        /// 查询到货通知列表（包含供应商和入库单信息）
        /// </summary>
        /// <param name="dto">查询条件DTO</param>
        /// <returns>分页查询结果</returns>
        public async Task<WebResponseContent> QueryInformListAsync(SelectInformDTO dto)
        {
            // 联查Supplier和PurchaseWarehousing
            var query = from inform in _inform.GetAll()
                        join supplier in _supplier.GetAll() on inform.Id equals supplier.Id into s
                        from supplier in s.DefaultIfEmpty()
                        join warehousing in _warehousing.GetAll() on inform.Id equals warehousing.Id into w
                        from warehousing in w.DefaultIfEmpty()
                        select new InformDTO
                        {
                            Id = inform.Id,
                            InformCode = inform.InformCode,
                            InformName = inform.InformName,
                            PurchaseCode = inform.PurchaseCode,
                            SupplierCode = inform.SupplierCode,
                            SupplierName = supplier.SupplierName,
                            LinkMan = inform.LinkMan,
                            Phone = inform.Phone,
                            DeliveryDate = inform.DeliveryDate,
                            Descs = inform.Descs,
                            InboundListCode = inform.InboundListCode,
                            States = warehousing.States,
                        };

            // 按条件过滤
            if (!string.IsNullOrEmpty(dto.InformCode))
                query = query.Where(x => x.InformCode == dto.InformCode);
            if (!string.IsNullOrEmpty(dto.InformName))
                query = query.Where(x => x.InformName.Contains(dto.InformName));
            if (!string.IsNullOrEmpty(dto.PurchaseCode))
                query = query.Where(x => x.PurchaseCode == dto.PurchaseCode);
            if (!string.IsNullOrEmpty(dto.SupplierName))
                query = query.Where(x => x.SupplierName.Contains(dto.SupplierName));
            if (dto.DeliveryDate != null)
                query = query.Where(x => x.DeliveryDate == dto.DeliveryDate);
            if (dto.States != null)
                query = query.Where(x => x.States == dto.States);

            int totalCount = query.Count();
            var data = query.Skip((dto.PageIndex - 1) * dto.PageSize).Take(dto.PageSize).ToList();

            var result = new VOL.Domain.Dto.PagedResponseDto<InformDTO>
            {
                Data = data,
                TotalCount = totalCount,
                PageSize = dto.PageSize,
                CurrentPage = dto.PageIndex,
                TotalPages = (int)Math.Ceiling((double)totalCount / dto.PageSize)
            };
            return WebResponseContent.Instance.OK("查询成功", result);
        }

        /// <summary>
        /// 编辑到货通知
        /// </summary>
        /// <param name="inform">到货通知实体</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> UpdateInformAsync(Inform inform)
        {
            var result = await _inform.Update(inform);
            return result > 0 ? WebResponseContent.Instance.OK("编辑成功") : WebResponseContent.Instance.Error("编辑失败");
        }
    }
}
